/*==============================================================================
案例1：员工流失预测 - 数据准备
================================================================================

业务场景：
某公司人力资源部门希望预测哪些员工可能离职，以便提前采取挽留措施。

数据说明：
使用 Stata 内置的 nlsw88.dta（National Longitudinal Survey of Women）数据集
进行模拟员工流失分析。

变量说明：
- wage: 工资（小时）
- hours: 工作小时数
- ttl_exp: 总工作经验（年）
- tenure: 当前公司工作年限
- age: 年龄
- grade: 教育年限
- married: 婚姻状况
- union: 是否加入工会
- south: 是否在南部工作
- industry: 行业
- occupation: 职业

目标变量：
我们将创建一个模拟的离职变量（attrition）

作者：Stata ML Course
日期：2025-11-03
==============================================================================*/

clear all
set more off
capture log close
log using "output/cases/case01_data_prep.log", replace

*------------------------------------------------------------------------------
* 1. 加载数据
*------------------------------------------------------------------------------

display "=" * 80
display "案例1：员工流失预测 - 数据准备"
display "=" * 80
display ""

* 加载 Stata 内置数据
sysuse nlsw88, clear

display "原始数据概览："
describe
display ""
display "样本量: " _N
display "变量数: " c(k)

*------------------------------------------------------------------------------
* 2. 创建目标变量（模拟离职）
*------------------------------------------------------------------------------

display ""
display "创建目标变量：员工离职（attrition）"
display "-" * 80

* 设置随机种子
set seed 20251103

* 创建离职概率（基于多个因素）
* 离职概率受以下因素影响：
* - 工资低 → 离职概率高
* - 工作年限短 → 离职概率高
* - 年龄年轻 → 离职概率高
* - 工作时间长 → 离职概率高

* 标准化变量
egen wage_std = std(wage)
egen tenure_std = std(tenure)
egen age_std = std(age)
egen hours_std = std(hours)

* 计算离职倾向得分
gen attrition_score = -0.3 * wage_std ///
                     -0.2 * tenure_std ///
                     -0.15 * age_std ///
                     + 0.1 * hours_std ///
                     + runiform() * 0.5

* 转换为概率
egen score_min = min(attrition_score)
egen score_max = max(attrition_score)
gen attrition_prob = (attrition_score - score_min) / (score_max - score_min)

* 创建二元离职变量（30%离职率）
gen attrition = (attrition_prob > 0.7)
label variable attrition "员工是否离职 (1=是, 0=否)"

* 检查离职率
tab attrition
summarize attrition

* 清理临时变量
drop wage_std tenure_std age_std hours_std attrition_score ///
     score_min score_max attrition_prob

*------------------------------------------------------------------------------
* 3. 特征工程
*------------------------------------------------------------------------------

display ""
display "特征工程："
display "-" * 80

* 3.1 工资相关特征
gen wage_per_hour = wage
label variable wage_per_hour "小时工资"

gen monthly_income = wage * hours * 4
label variable monthly_income "月收入（估算）"

* 3.2 工作经验特征
gen exp_tenure_ratio = ttl_exp / (tenure + 1)
label variable exp_tenure_ratio "总经验/当前公司年限比"

gen is_new_employee = (tenure < 1)
label variable is_new_employee "是否新员工（<1年）"

gen is_veteran = (tenure >= 5)
label variable is_veteran "是否资深员工（>=5年）"

* 3.3 年龄相关特征
gen age_group = 1 if age < 30
replace age_group = 2 if age >= 30 & age < 40
replace age_group = 3 if age >= 40 & age < 50
replace age_group = 4 if age >= 50 & age < .
label define age_group_lbl 1 "20-29岁" 2 "30-39岁" 3 "40-49岁" 4 "50岁以上"
label values age_group age_group_lbl
label variable age_group "年龄组"

* 3.4 工作强度
gen is_overwork = (hours > 45)
label variable is_overwork "是否过度工作（>45小时/周）"

* 3.5 教育水平
gen education_level = 1 if grade < 12
replace education_level = 2 if grade == 12
replace education_level = 3 if grade > 12 & grade < 16
replace education_level = 4 if grade >= 16 & grade < .
label define edu_lbl 1 "高中以下" 2 "高中" 3 "大专" 4 "本科及以上"
label values education_level edu_lbl
label variable education_level "教育水平"

*------------------------------------------------------------------------------
* 4. 数据质量检查
*------------------------------------------------------------------------------

display ""
display "数据质量检查："
display "-" * 80

* 缺失值检查
misstable summarize

* 处理缺失值
display ""
display "处理缺失值..."

* 删除关键变量缺失的观测
drop if missing(wage, hours, ttl_exp, tenure, age, grade)

display "处理后样本量: " _N

*------------------------------------------------------------------------------
* 5. 描述性统计
*------------------------------------------------------------------------------

display ""
display "描述性统计："
display "-" * 80

* 连续变量
summarize wage hours ttl_exp tenure age grade monthly_income

* 分类变量
tab attrition
tab married attrition, row
tab union attrition, row
tab south attrition, row

*------------------------------------------------------------------------------
* 6. 数据可视化
*------------------------------------------------------------------------------

display ""
display "生成数据可视化..."
display "-" * 80

* 6.1 离职率分布
graph bar (count), over(attrition) ///
    title("员工离职分布") ///
    ytitle("人数") ///
    blabel(bar, format(%9.0f)) ///
    scheme(s2color)
graph export "output/cases/figures/case01_attrition_dist.png", replace

* 6.2 工资与离职关系
graph box wage, over(attrition) ///
    title("工资水平与离职关系") ///
    ytitle("小时工资") ///
    scheme(s2color)
graph export "output/cases/figures/case01_wage_attrition.png", replace

* 6.3 工作年限与离职关系
graph box tenure, over(attrition) ///
    title("工作年限与离职关系") ///
    ytitle("工作年限（年）") ///
    scheme(s2color)
graph export "output/cases/figures/case01_tenure_attrition.png", replace

* 6.4 年龄与离职关系
graph bar (mean) attrition, over(age_group) ///
    title("不同年龄组的离职率") ///
    ytitle("离职率") ///
    blabel(bar, format(%9.2f)) ///
    scheme(s2color)
graph export "output/cases/figures/case01_age_attrition.png", replace

* 6.5 教育水平与离职关系
graph bar (mean) attrition, over(education_level) ///
    title("不同教育水平的离职率") ///
    ytitle("离职率") ///
    blabel(bar, format(%9.2f)) ///
    scheme(s2color)
graph export "output/cases/figures/case01_education_attrition.png", replace

* 6.6 工作时长与离职关系
scatter attrition hours, jitter(5) ///
    title("工作时长与离职关系") ///
    xtitle("每周工作小时数") ytitle("是否离职") ///
    scheme(s2color)
graph export "output/cases/figures/case01_hours_attrition.png", replace

* 6.7 相关性热图（使用散点图矩阵）
graph matrix wage hours ttl_exp tenure age, ///
    title("关键变量相关性矩阵") ///
    scheme(s2color)
graph export "output/cases/figures/case01_correlation_matrix.png", replace

*------------------------------------------------------------------------------
* 7. 保存处理后的数据
*------------------------------------------------------------------------------

display ""
display "保存数据..."
display "-" * 80

* 保留需要的变量
keep attrition wage hours ttl_exp tenure age grade married union south ///
     industry occupation monthly_income exp_tenure_ratio is_new_employee ///
     is_veteran age_group is_overwork education_level

* 保存数据
save "data/cases/employee_attrition.dta", replace
export delimited "data/cases/employee_attrition.csv", replace

display ""
display "数据准备完成！"
display "保存位置："
display "  - data/cases/employee_attrition.dta"
display "  - data/cases/employee_attrition.csv"
display ""
display "生成图表："
display "  - output/cases/figures/case01_*.png (7个图表)"

*------------------------------------------------------------------------------
* 8. 生成数据报告
*------------------------------------------------------------------------------

display ""
display "=" * 80
display "数据准备报告"
display "=" * 80
display ""
display "数据集: employee_attrition.dta"
display "观测数: " _N
display "变量数: " c(k)
display ""

display "目标变量分布："
tab attrition

display ""
display "关键特征统计："
summarize wage monthly_income tenure ttl_exp age

log close

display ""
display "日志文件保存在: output/cases/case01_data_prep.log"
display ""
display "下一步: 运行 case01_employee_attrition_model.do 进行建模"

